﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tab2Sql.Classes.Core;
using Tab2Sql.Classes;

namespace Tab2Sql
{
    public partial class ShowGrid : Form
    {
        /// <summary>
        /// Options object.
        /// </summary>
        public Options MyOptions = new Options();
        
        /// <summary>
        /// main object. 
        /// </summary>


        BindingSource bs = new BindingSource();
        DataTable dt;
        /// <summary>
        /// Constructor. 
        /// </summary>
        public ShowGrid ()
        {
            InitializeComponent ();
            MyOptions.Delimiter = "\t";
            dt = new DataTable ();
            navControls.BindingSource = bs;
            bs.DataSource = dt;
            myGridView.DataSource = bs;
            myGridView.Refresh ();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="myOtherTable">
        /// A <see cref="DataTable"/>
        /// </param>
        public ShowGrid (DataTable myOtherTable)
        {
            InitializeComponent ();
            dt = myOtherTable;
            navControls.BindingSource = bs;
            bs.DataSource = dt;
            myGridView.DataSource = bs;
            myGridView.Refresh ();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="myOtherTable">
        /// A <see cref="xTab2Sql"/>
        /// </param>
        public ShowGrid(xTab2Sql myOtherTable)
        {

            InitializeComponent();
            dt = myOtherTable.OurDataTable;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dt.Columns[i].ColumnMapping = MappingType.Element;
            }
            
            navControls.BindingSource = bs;
            
            bs.DataSource = dt.AsDataView();
            myGridView.AutoGenerateColumns = true;
            myGridView.DataSource = bs;
            
            
            myGridView.Refresh();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Get's datatable from gridview. 
        /// </summary>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public DataTable ExtractDataTable(DataGridView dgv)
        {
            BindingSource BSrc = dgv.DataSource as BindingSource;
            DataTable dtSrc = BSrc.DataSource as DataTable;
            DataTable dtDest = null;
            if (dtSrc != null)
            {
                dtDest = new DataTable();
                dtDest.Merge(dtSrc);
            }
            return dtDest;
        }

        private void button2_Click (object sender, EventArgs e)
        {
            MessageBox.Show ("NOT IMPLEMENTED!");
            
            //myGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
            //MyOptions.Delimiter = "\t";
            //DataTable dt = new DataTable();
            //dt = ExtractDataTable(myGridView);
            //DataTable2SQL DTS = new DataTable2SQL(dt);
            //DTS.MyOptions = MyOptions;
            //DTS.MyObject = MyObject;
            //DTS.GetSQL();
            //System.Text.StringBuilder endS = new StringBuilder();
            //endS.Append(DTS.ToString());
            //dt = null;
            //RichTextBox TB = ((MainForm)this.Owner).outStr;
            //TB.Text = endS.ToString();
            //this.Close();
            //this.Dispose();

        }
        private void bindingNavigator1_RefreshItems(object sender, EventArgs e)
        {
            // wtf
        }

        private void showGrid_Load(object sender, EventArgs e)
        {
         
   
  

        }

        private void button4_Click(object sender, EventArgs e)
        {

            

            
        }
    }
}
